define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'project/index' + location.search,
                    add_url: 'project/add',
                    edit_url: 'project/edit',
                    del_url: 'project/del',
                    multi_url: 'project/multi',
                    table: 'project'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'pname', title: __('Pname')},
                        {field: 'manager_name', title: __('项目经理')},
                        {field: 'industry_id', title: __('Industry_id')},
                        {field: 'startdate', title: __('Startdate'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'enddate', title: __('Enddate'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'category_names', title: __('Category_ids')},
                        {field: 'pstate', title: __('Pstate'), searchList: {"0":__('Pstate 0'),"1":__('Pstate 1')}, formatter: Table.api.formatter.normal},
                        {field: 'computation', title: __('Computation')},
                        {field: 'contactname', title: __('Contactname')},
                        {field: 'pmobile', title: __('Pmobile')},
                        {field: 'usedate', title: __('Usedate'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'spend_progress', title: __('项目进度%')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();

            //初始化编辑页面的人才列表表格
            var categoryTable = $("#table_categories");

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    table: 'project_categories',
                    category_url: 'project/categories/project_id/' + categoryTable.data('record-id') + location.search
                },
                //隐藏分页
                pagination:false
            });

            categoryTable.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.category_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        //{checkbox: false, operate: false},
                        //{field: 'id', title: 'ID', operate: false},
                        {field: 'name', title: __('职位角色'), operate: false},
                        {field: 'assign_status', title: __('匹配状态'), operate: false},
                        {field: 'assign_user', title: __('已匹配专家'), operate: false},
                        {field: 'assign_time', title: __('匹配时间'), operate: false},
                        {field: 'work_status_name', title: __('工作状态'), operate: false},
                        {field: 'employ_price', title: __('雇佣价格'), operate: false},
                        {field: 'is_charge_name', title: __('是否计费'), operate: false},
                        {field: 'operate', title: __('Operate'), table: categoryTable, events: Table.api.events.operate,
                            buttons:[
                                {
                                    name: 'assignuser',
                                    text: __('匹配专家'),
                                    title: __('匹配专家'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'project/assignUser/project_id/' + categoryTable.data('record-id'),
                                    callback: function (data) {
                                        //Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.is_assign == 1) {   //已分配
                                            return false;
                                        } else {    //未分配
                                            return true;
                                        }
                                    },
                                    success: function (data, ret) {
                                        //刷新表格
                                        categoryTable.bootstrapTable('refresh');
                                    }
                                },
                                {
                                    name: 'editassign',
                                    text: __('调整分配'),
                                    title: __('调整分配'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'project/editAssign/project_id/' + categoryTable.data('record-id'),
                                    callback: function (data) {
                                        //Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    /*visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.is_assign == 1) {   //已分配
                                            return false;
                                        } else {    //未分配
                                            return true;
                                        }
                                    },*/
                                    success: function (data, ret) {
                                        //刷新表格
                                        categoryTable.bootstrapTable('refresh');
                                    }
                                },
                                {
                                    name: 'deluser',
                                    text: __('退出该项目'),
                                    title: __('退出该项目'),
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    icon: 'fa fa-list',
                                    url: 'project/deluser',
                                    callback: function (data) {
                                        //Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.is_assign == 1) {   //已分配
                                            if (row.work_status == 3) { //退出
                                                return false;
                                            }
                                            return true;
                                        } else {    //未分配
                                            return false;
                                        }
                                    },
                                    success: function (data, ret) {
                                        //刷新表格
                                        categoryTable.bootstrapTable('refresh');
                                    }
                                },
                                {
                                    name: 'delrecord',
                                    text: __('删除'),
                                    title: __('删除'),
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    icon: 'fa fa-list',
                                    url: 'project/delrecord',
                                    callback: function (data) {
                                        //Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.is_assign == 1) {   //已分配
                                            return false;
                                        } else {    //未分配
                                            return true;
                                        }
                                    },
                                    success: function (data, ret) {
                                        //刷新表格
                                        categoryTable.bootstrapTable('refresh');
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(categoryTable);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
        assignuser: function() {    // 分配专家
            var projectId = Config.project.id;
            var categoryId = Config.project.category_id;
            var assignId = Config.project.assign_id;

            //初始化编辑页面的人才列表表格
            var table = $("#table_assign_user");

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    table: 'table_assign_user',
                    index_url: 'user/user/categoryUsers' + location.search
                },
                searchFormVisible: true
            });

            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        //{checkbox: false, operate: false},
                        {field: 'id', title: __('Id'), sortable: true},
                        // {field: 'group.name', title: __('Group')},
                        {field: 'hua_name', title: __('花名'), operate: false},
                        {field: 'nickname', title: __('专家名称'), operate: false},
                        {field: 'category_name', title: __('职位/角色'), operate: false},
                        {field: 'skill', title: __('擅长领域'), operate: false},
                        {field: 'createtime', title: __('注册时间'), operate: false},
                        {field: 'work_status_name', title: __('工作状态'), operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons:[
                                {
                                    name: 'doassign',
                                    text: __('进入项目'),
                                    title: __('进入项目'),
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    icon: 'fa fa-list',
                                    url: 'project/doassign/assign_id/' + assignId,
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    },
                                    success: function (data, ret) {
                                        //$(".btn-refresh").trigger("click");
                                        //刷新表格
                                        table.bootstrapTable('refresh');
                                        //浮窗口的表格刷新
                                        parent.$("a.btn-refresh").trigger("click");
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.operate}
                    ]
                ],
                searchFormVisible: true,
                searchFormTemplate: 'assignsearchformtpl'
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            Controller.api.bindevent(); //渲染form
        },
        addposition:function() {    //添加职位页面
            Controller.api.bindevent();
        },
        deluser: function () {  //退出项目
            Controller.api.bindevent();
        },
        delrecord: function () {    //删除
            Controller.api.bindevent();
        },
        editassign: function () {
            Controller.api.bindevent();
        }
    };
    return Controller;
});